﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Owl.Contracts;
using System.Data.Entity;
using System.Diagnostics;
using Owl.Questionnarie.Entities;

namespace Owl.Questionnarie.DAL
{
    public class ChoiceDAL : ICrud<Choices, Guid>
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public Choices GetById(Guid key)
        {
            Trace.WriteLine("ChoiceDAL", "Iniciando GetById");
            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {

                    Choices q = (from choice in context.Choices
                                 where choice.ChoiceId == key
                                 select choice).FirstOrDefault();

                    Trace.WriteLine("ChoiceDAL", "Retornando objeto");
                    return q;
                }

            }
            catch (Exception ex)
            {
                Trace.WriteLine("ChoiceDAL", ex.Message);
                throw ex;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<Choices> GetAll()
        {
            Trace.WriteLine("ChoiceDAL", "Iniciando GetAll");
            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {

                    List<Choices> q = (from choice in context.Choices
                                       select choice).ToList();

                    Trace.WriteLine("ChoiceDAL", "Retornando objeto");
                    return q;
                }

            }
            catch (Exception ex)
            {
                Trace.WriteLine("ChoiceDAL", ex.Message);
                throw ex;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public Choices Insert(Choices entity)
        {
            Trace.WriteLine("ChoiceDAL", "Iniciando insert");
            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {

                    entity.ChoiceId = Guid.NewGuid();
                    context.AddToChoices(entity);
                    context.SaveChanges();
                    Trace.WriteLine("ChoiceDAL", "Inserção realizada");

                    Trace.WriteLine("ChoiceDAL", "Retornando objeto");
                    return entity = (from choice in context.Choices
                                     where choice.ChoiceId == entity.ChoiceId
                                     select choice).FirstOrDefault();
                }

            }
            catch (Exception ex)
            {
                Trace.WriteLine("ChoiceDAL", ex.Message);
                throw ex;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        public void Update(Choices entity)
        {
            Trace.WriteLine("ChoiceDAL", "Iniciando update");
            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {

                    Choices q = (from choice in context.Choices
                                 where choice.ChoiceId == entity.ChoiceId
                                 select choice).FirstOrDefault();

                    q.Description = entity.Description;
                    context.SaveChanges();
                    Trace.WriteLine("ChoiceDAL", "Alteração realizada");
                }

            }
            catch (Exception ex)
            {
                Trace.WriteLine("ChoiceDAL", ex.Message);
                throw ex;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        public void Delete(Guid key)
        {
            Trace.WriteLine("ChoiceDAL", "Iniciando deleção");
            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {

                    Choices q = (from choice in context.Choices
                                 where choice.ChoiceId == key
                                 select choice).FirstOrDefault();

                    context.DeleteObject(q);
                    context.SaveChanges();
                    Trace.WriteLine("ChoiceDAL", "Deleção realizada");
                }

            }
            catch (Exception ex)
            {
                Trace.WriteLine("ChoiceDAL", ex.Message);
                throw ex;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public List<Choices> GetByString(String text)
        {
            Trace.WriteLine("ChoiceDAL", "Iniciando GetByString");
            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {

                    List<Choices> q = (from choice in context.Choices
                                       where choice.Description.Contains(text)
                                       select choice).ToList();

                    Trace.WriteLine("ChoiceDAL", "Retornando objeto");
                    return q;
                }

            }
            catch (Exception ex)
            {
                Trace.WriteLine("ChoiceDAL", ex.Message);
                throw ex;
            }
        }
    }
}
